package com.fly.controller;

import com.fly.bean.Role;
import com.fly.bean.SystemPropertiesValue;
import com.fly.service.RoleService;
import com.fly.util.Msg;
import com.fly.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    /**
     *  分页显示角色信息
     * @return
     */
    @RequestMapping("/showAllRole")
    @ResponseBody
    public Msg getAllRoleAndShowByPage(Page page){
        //查询得到的角色数量
        Integer totalCount = roleService.selectRoleNumber();
//        System.out.println("totalCount" + totalCount);
        //分页对象中设置总数量为查询的数量
        page.setCount(totalCount);

        List<Role> roleList = roleService.getAllRole(page);

        if (roleList.size() > 0){
            return Msg.success().add("count",page.getCount()).add("roleList",roleList);
        }else{
            return Msg.fail();
        }
    }

    /**
     * 模糊查询分页显示角色信息
     * @param page
     * @param roleName
     * @param sysProValueName
     * @return
     */
    @RequestMapping("/fuzzyQueryShowRole")
    @ResponseBody
    public Msg fuzzyQueryGetRoleShowByPage(Page page,String roleName,String sysProValueName){
        String newRoleName = "%" + roleName + "%";

        //查询得到的角色数量
        Integer totalCount = roleService.selectRoleNumberByFuzzyQuery(newRoleName,sysProValueName);
        System.out.println("totalCount" + totalCount);
        //分页对象中设置总数量为查询的数量
        page.setCount(totalCount);

        List<Role> roleList = roleService.selectRoleByRoleNameOrSysProValueName(page,newRoleName,sysProValueName);

        if (roleList.size() > 0){
            return Msg.success().add("count",page.getCount()).add("roleList",roleList);
        }else{
            return Msg.fail();
        }
    }

    /**
     * 判断角色编号是否存在
     * 用到两次
     * @param roleNo
     * @return
     */
    @RequestMapping("/isExistsRoleNo")
    @ResponseBody
    public Msg isExistsRoleNo(String roleNo){
        //判断角色编号是否存在
        String roleNos = roleService.selectRoleNo(roleNo);

        if(roleNos == null){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }

    /**
     *  判断角色名称是否存在
     *  用到两次
     * @param roleName
     * @return
     */
    @RequestMapping("/isExistsRoleName")
    @ResponseBody
    public Msg isExistsRoleName(String roleName){
        //判断角色名称是否存在
        String newRoleName = roleService.selectRoleName(roleName);
        if(newRoleName == null){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }

    /**
     * 修改角色信息
     * @param roleNo
     * @param roleName
     * @param sysProValueName
     * @param roleId
     * @return
     */
    @RequestMapping("/modifyRoleNoAndRoleName")
    @ResponseBody
    public Msg modifyRoleNoAndRoleName(String roleNo, String roleName, String sysProValueName ,Integer roleId){
        //判断角色编号是否存在
        String roleNos = roleService.selectRoleNo(roleNo);
        //判断角色名称是否存在
        String newRoleName = roleService.selectRoleName(roleName);

        //根据角色Id查询角色名称
        String selectRoleName = roleService.selectRoleNameByRoleId(roleId);
        //通过角色Id查找角色编号
        String selectRoleNo = roleService.selectRoleNoByRoleId(roleId);

        if ((roleNos != null && !selectRoleNo.equals(roleNos)) || (newRoleName != null && !selectRoleName.equals(newRoleName))){
            return Msg.fail();
        }else {
            Integer roleState = roleService.selectRoleStateBySysProIdAndSysProValueName(sysProValueName);

            Role role = new Role();
            role.setRoleId(roleId);
            role.setRoleNo(roleNo);
            role.setRoleName(roleName);

            SystemPropertiesValue systemPropertiesValue = new SystemPropertiesValue();
            systemPropertiesValue.setId(roleState);

            role.setSystemPropertiesValue(systemPropertiesValue);
            Integer result = roleService.modifyRoleNoAndRoleName(role);

            if (result > 0){
                return Msg.success();
            }else{
                return Msg.fail();
            }
        }
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @RequestMapping("/deleteRole")
    @ResponseBody
    public Msg deleteRoleByRoleId(Integer roleId){
        //根据角色Id查询角色名称
        String roleName = roleService.selectRoleNameByRoleId(roleId);
        //如果角色名称是超级管理员就不能删除
        if ("超级管理员".equals(roleName)){
            return Msg.fail();
        }else{
            Integer result = roleService.deleteRole(roleId);
            if (result > 0){
                return Msg.success();
            }else{
                return Msg.fail();
            }
        }
    }

    /**
     * 添加角色信息
     * 首先要用到上面是否存在角色编号和角色名称的方法
     * @param roleNo
     * @param roleName
     * @param userId
     * @param sysProValueName
     * @return
     */
    @RequestMapping("/addRole")
    @ResponseBody
    public Msg addRole(String roleNo,String roleName,Integer userId,String sysProValueName){
        //判断角色编号是否存在
        String roleNos = roleService.selectRoleNo(roleNo);
        //判断角色名称是否存在
        String newRoleName = roleService.selectRoleName(roleName);

        if (roleNos != null || newRoleName != null){
            return Msg.fail();
        }else{
            //根据sysProValueName得到sysProId用于添加角色使用,还有用于得到roleState的判断条件
            Integer sysProId = roleService.selectSysProIdBySysProName("角色状态配置");

            //根据sysProValueName和sysProId得到systempropertiesvalue中的id值用于得到role表中的roleState值
            Integer roleState = roleService.selectIdBySysProValueNameAndSysProId(sysProValueName,sysProId);

            //添加角色
            Integer result = roleService.addRole(roleNo,roleName,userId,roleState,sysProId);

            if (result > 0){
                return Msg.success();
            }else{
                return Msg.fail();
            }
        }
    }
}
